package com.sunyur.tailor.channel.web.outer.purchase.controller;

import com.alibaba.fastjson.JSON;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.service.inner.workflow.param.WorkFlowSyncTaskParam;
import com.sunyur.tailor.channel.service.middle.purchase.MiddlePurchaseService;
import com.sunyur.tailor.channel.web.outer.purchase.dto.WorkFlowSyncTaskDTO;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.beanutils.BeanUtilsBean2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.util.Objects;

/**
 * @Author pengbing
 * @Description //接收外部审批结果
 * @Date 16:36 2021/12/30
 * @Param
 * @return
 **/
@Log4j2
@RestController
@RequestMapping("/outer/apply")
public class PurchaseApplyController {

    @Autowired
    MiddlePurchaseService middlePurchaseService;

    /**
     *采购申请审核
     */
    @PostMapping(value = "/audit", produces = MediaType.APPLICATION_JSON_VALUE)
    public ChannelResult audit(@RequestBody @Valid WorkFlowSyncTaskDTO workFlowSyncTaskDTO) throws InvocationTargetException, IllegalAccessException {
        //参数校验
        log.info("PurchaseApplyController audit param:{}", JSON.toJSONString(workFlowSyncTaskDTO));
        ChannelResult result = new ChannelResult<>();
        if (Objects.isNull(workFlowSyncTaskDTO)) {
            log.error("PurchaseApplyController audit param is null");
            result.setError(MiddleResultEnum.PARAM_NULL.getCode(), MiddleResultEnum.PARAM_NULL.getValue());
            return result;
        }
        //外部审核
        WorkFlowSyncTaskParam workFlowSyncTaskParam = new WorkFlowSyncTaskParam();
        BeanUtilsBean2.getInstance().copyProperties(workFlowSyncTaskParam, workFlowSyncTaskDTO);
        result = middlePurchaseService.syncAudit(workFlowSyncTaskParam);
        log.info("PurchaseApplyController audit return:{}", JSON.toJSONString(result));
        return result;
    }

}
